package com.readbook.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.readbook.dao.UaccountDao;
import com.readbook.dao.WechartDao;
import com.readbook.util.HttpPostUrl;
import com.readbook.util.SecCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@EnableScheduling
public class WechartService {
    @Autowired
    private WechartDao wechartDao;
    @Autowired
    private UaccountDao uaccountDao;

    public List<Map<String,Object>>getuserBindStu(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.getuserBindStu(gmap);
        return list1;
    }

    /**
     * @author 牟明艳
     * @comments 远程获取用户基本信息
     * @time 2019年8月17日12:13:04
     */
    public Map<String,Object>getUserInfoThird(String dlzh,String dlma)
    {
        Map<String, Object> remap = new HashMap<String, Object>();
        Map<String, Object> mapParam = new HashMap<String, Object>();
        mapParam.put("username", dlzh);
        mapParam.put("password",dlma);
        mapParam.put("user_type",1);//1学生 2教师
        String pathUrl = "https://gyhx-api.86358.com/third/getAccessToken";
        String result = HttpPostUrl.sendPost(pathUrl, mapParam);
        result=HttpPostUrl.decodeUnicode(result);
        Map<String,Object>resultmap= JSON.parseObject(result);

        if(resultmap.get("code").toString().equals("1000001")){
            //用户名不存在 看看是否为教师账号
            mapParam.remove("user_type");//1学生 2教师
            mapParam.put("user_type",2);//1学生 2教师
            result = HttpPostUrl.sendPost(pathUrl, mapParam);
            result=HttpPostUrl.decodeUnicode(result);
            Map<String,Object>resultmap1=JSON.parseObject(result);
            if(resultmap1.get("code").toString().equals("1000001")){
                //即不是学生账号 也不是教师账号
                remap.put("msg","账号不存在!");
                remap.put("result",false);
            }else if(resultmap1.get("code").toString().equals("1000002")){
                //密码错误
                remap.put("msg","密码错误,请重试!");
                remap.put("result",false);
            }else{
                //成功 账号为教师
                String tokendata=resultmap1.get("data").toString();
                Map<String,Object> tokenmaps=JSON.parseObject(tokendata);
                String token=tokenmaps.get("token").toString();

                String teaurl="https://gyhx-api.86358.com/third/teacherInfo";
                String tearesult=HttpPostUrl.httpURLConectionGET(teaurl,token);

                Map<String,Object>teamaps=JSON.parseObject(tearesult);
                if(!teamaps.get("code").toString().equals("0")){
                    remap.put("msg",teamaps.get("msg").toString());
                    remap.put("result",false);
                }else{
                    String teadatas=teamaps.get("data").toString();
                    Map<String,Object> teadatamap=JSON.parseObject(teadatas);
                    remap.put("msg","登录成功!");
                    remap.put("result",true);
                    remap.put("name",teadatamap.get("name"));
                    remap.put("sex",teadatamap.get("sex"));
                    remap.put("identity",teadatamap.get("identity_name"));
                    remap.put("user_type",2);
                    if(teadatamap.get("banji_name")!=null && !teadatamap.get("banji_name").toString().equals("")){
                        String banji_name=teadatamap.get("banji_name").toString();
                        String darr[]= banji_name.split("年级");
                        String nj=getXxNUM(darr[0]);
                        String bj=darr[1].replaceAll("班","");
                        remap.put("nj",nj);
                        remap.put("bj",bj);
                    }
                }

            }
        }else if(resultmap.get("code").toString().equals("1000002")){
            //密码错误
            remap.put("msg","密码错误,请重试!");
            remap.put("result",false);
        }else{
            //成功 账号为学生
            String tokendata=resultmap.get("data").toString();
            Map<String,Object> tokenmaps=JSON.parseObject(tokendata);
            String token=tokenmaps.get("token").toString();
            String stuurl = "https://gyhx-api.86358.com/third/studentInfo";
            String sturesult=HttpPostUrl.httpURLConectionGET(stuurl,token);

            Map<String,Object>stumaps=JSON.parseObject(sturesult);
            if(!stumaps.get("code").toString().equals("0")){
                remap.put("msg",stumaps.get("msg").toString());
                remap.put("result",false);
            }else{
                remap.put("msg","登录成功!");
                remap.put("result",true);
                remap.put("user_type",1);
                String studatas=stumaps.get("data").toString();
                List<Map<String,Object>> liststu = (List<Map<String,Object>>) JSONArray.parse(studatas);
                remap.put("stusize",liststu.size());
                if(liststu.size()==1){
                    remap.put("name",liststu.get(0).get("name"));
                    remap.put("sex",liststu.get(0).get("sex"));
                    remap.put("phone",liststu.get(0).get("phone"));
                    remap.put("identity",liststu.get(0).get("member_post_name"));
                    remap.put("bj",liststu.get(0).get("banji_num"));
                    String ruxn=liststu.get(0).get("year").toString();

                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH )+1;
                    int nj=year-Integer.parseInt(ruxn);
                    if(month<8){
                        nj=nj-1;//因为上半学期还没有升学
                    }
                    remap.put("nj",nj);
                }else{
                    List<Map<String,Object>> listmaps=new ArrayList<>();
                    for(int m=0;m<liststu.size();m++){
                        Map<String,Object> struformap=new HashMap<>();
                        struformap.put("name",liststu.get(m).get("name"));
                        struformap.put("sex",liststu.get(m).get("sex"));
                        struformap.put("phone",liststu.get(m).get("phone"));
                        struformap.put("identity",liststu.get(m).get("member_post_name"));
                        struformap.put("bj",liststu.get(m).get("banji_num"));
                        String ruxn=liststu.get(m).get("year").toString();

                        Calendar cal = Calendar.getInstance();
                        int year = cal.get(Calendar.YEAR);
                        int month = cal.get(Calendar.MONTH )+1;
                        int nj=Integer.parseInt(ruxn)-year;
                        if(month<8){
                            nj=nj-1;//因为上半学期还没有升学
                        }
                        struformap.put("nj",nj);
                        listmaps.add(struformap);
                    }
                    remap.put("stus",listmaps);
                }
            }
        }
        return remap;
    }



    public String getXxNUM(String dxnum){
        String numarr[]={"零","一","二","三","四","五","六","七","八","九"};
        String xinum="";
        for(int j=0;j<numarr.length;j++){
            if(numarr[j]==dxnum){
                xinum=j+"";
            }
        }
        return xinum;
    }


    public  int insertwxbangding(Map<String,Object>gmap)
    {
        return wechartDao.insertwxbangding(gmap);
    }


    public List<Map<String,Object>>getUsidBydlzh(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.getUsidBydlzh(gmap);
        return list1;
    }








    public List<Map<String,Object>>getwxuserBindStu(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.getwxuserBindStu(gmap);
        return list1;
    }




    public List<Map<String,Object>>getUaccountBydlzh(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.getUaccountBydlzh(gmap);
        return list1;
    }

    public List<Map<String,Object>>getStuUaccountBynamezh(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.getStuUaccountBynamezh(gmap);
        return list1;
    }

    public int updateUaccountByStuzh(Map<String,Object>gmap){
        wechartDao.insertwxbangding(gmap);
        int m=uaccountDao.updateUaccountByStuzh(gmap);
        return m;
    }


    public int updateUaccountByTeazh(Map<String,Object>gmap){
        wechartDao.insertwxbangding(gmap);
        int m=uaccountDao.updateUaccountByTeazh(gmap);
        return m;
    }


    public int insert_uaccount(Map<String,Object>gmap)
    {
        wechartDao.insertwxbangding(gmap);
        int m=uaccountDao.insertUaccount(gmap);
        return m;
    }


/*


    public  int insert_teauaccount(Map<String,Object>gmap)
    {
        Map<String, Object> getmap = new HashMap<>();
        getmap.put("name", gmap.get("name"));
        getmap.put("zw", gmap.get("identity"));
        getmap.put("nj", gmap.get("nj"));
        getmap.put("bj", gmap.get("bj"));
        getmap.put("usid", gmap.get("usid"));
        getmap.put("dlzh", gmap.get("dlzh"));
        getmap.put("dlma", gmap.get("dlma"));
        getmap.put("sex", gmap.get("sex"));

        String phone=gmap.get("dlzh").toString();
        List<Map<String,Object>>teacher=uaccountDao.getTeaUaccountByzh(phone);
        int m=0;
        if(teacher.size()>0){
            for(int i=0;i<teacher.size();i++){
                Map<String, Object> wxmap = new HashMap<>();
                wxmap.put("usid",teacher.get(i).get("USID"));
                wxmap.put("wxid",gmap.get("wxid"));
                wechartDao.insertwxbangding(wxmap);
            }
            //修改
            m=uaccountDao.updateUaccountByTeazh(getmap);
        }else{
            getmap.put("wstate","2");
            getmap.put("jlsczt", "0");
            wechartDao.insertwxbangding(gmap);
            m=uaccountDao.insertUaccount(getmap);
        }
        return m;
    }
*/


    //编写具体的定时任务组件（@Component注解），并且在需要定时调度的方法上添加@Scheduled触发器
    //每隔5秒执行一次：*/5 * * * * ?
    //每隔1分钟执行一次：0 */1 * * * ?
    //每天23点执行一次：0 0 23 * * ?
    //每天凌晨1点执行一次：0 0 1 * * ?
    //每月1号凌晨1点执行一次：0 0 1 1 * ?
    //每月最后一天23点执行一次：0 0 23 L * ?
    //每周星期天凌晨1点实行一次：0 0 1 ? * L
    //在26分、29分、33分执行一次：0 26,29,33 * * * ?
    //每天的0点、13点、18点、21点都执行一次：0 0 0,13,18,21 * * ?
    @Scheduled(cron = "0 */90 * * * ?")
    public void refreshtoken()
    {
      SecCheck.getaccess_token();
    }




}
